<template>
    <div>
        <el-form :rules="rules" ref="hrForm" :model="hr" :label-width="formLabelWidth"
                 :label-position="labelPosition">
            <el-form-item prop="name" label="姓名">
                <el-input style="width: 250px" v-model="hr.name"></el-input>
            </el-form-item>
            <el-form-item prop="userface" label="头像">
                <el-upload
                        class="avatar-uploader"
                        action="/userface"
                        :show-file-list="false"
                        :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
<!--                    <img v-if="hr.userface" :src="hr.userface" class="avatar">-->
                    <el-avatar v-if="userface" size="large" :src="userface"></el-avatar>
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>

            <el-form-item prop="phone" label="手机">
                <el-input style="width: 250px" v-model="hr.phone"></el-input>
            </el-form-item>
            <el-form-item prop="telephone" label="电话">
                <el-input style="width: 250px" v-model="hr.telephone"></el-input>
            </el-form-item>
            <el-form-item prop="address" label="地址">
                <el-input style="width: 250px" v-model="hr.address"></el-input>
            </el-form-item>
            <el-form-item prop="username" label="用户名">
                <el-input style="width: 250px" v-model="hr.username"></el-input>
            </el-form-item>
            <el-form-item prop="remark" label="备注">
                <el-input
                        style="width: 250px"
                        type="textarea"
                        :rows="2"
                        placeholder="请输入内容"
                        v-model="hr.remark">
                </el-input>
            </el-form-item>
            <div style="margin-left: 150px;">
                <el-button type="primary" @click="doUpdate">提交修改</el-button>
            </div>
        </el-form>

    </div>
</template>

<script>
    export default {
        name: "CommonInfo",
        data(){
            return{
                hr:JSON.parse(window.sessionStorage.getItem("user")),
                userface:'',
                labelPosition: 'right',
                formLabelWidth: '80px',
                rules: {
                    name: [{required: true, message: '请输入姓名', trigger: 'blur'}],
                    phone: [{required: true, message: '请输入手机', trigger: 'blur'}],
                    telephone: [{required: true, message: '请输入电话', trigger: 'blur'}],
                    address: [{required: true, message: '请输入地址', trigger: 'blur'}],
                    username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
                },
            }
        },
        mounted() {
            // this.initHr();
            this.userface = this.hr.userface;
        },
        methods:{
            // initHr(){
            //     this.getRequest().then(resp=>{
            //         if (resp){
            //             this.hr = resp.obj;
            //             window.sessionStorage.setItem("user",JSON.stringify(resp.obj));
            //         }
            //     })
            // },
            doUpdate(){
                this.putRequest('/system/hr/',this.hr).then(resp=>{
                    if (resp){
                        window.sessionStorage.setItem("user",JSON.stringify(this.hr));
                    }
                })
            },
            handleAvatarSuccess(res, file) {
                if (res){
                    this.userface = res.obj;
                    this.hr.userface = res.obj;
                    window.sessionStorage.setItem("user",JSON.stringify(this.hr));
                }
            },
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isJPG) {
                    this.$message.error('上传头像图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            }
        }
    }
</script>

<style scoped>

</style>